package admin;

import java.util.*;

import javax.servlet.http.*;

import model.*;

import org.apache.struts.action.*;
import org.apache.struts.actions.*;

public class CommunityAction extends DispatchAction {
	
	/*
	 ********************CommunityAction 설명******************
     * Action을 상속받은 Action클래스. 
     * init() 메서드를 가지고 있음. 커뮤니티기본정보 취득용
     * search() - 커뮤니티정보 취득용
     * modify() - 커뮤니티수정작업
     **********************************************************
    */
	public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		//Form취득
		CommunityForm communityform = (CommunityForm)form; 
		setting(communityform);
		return mapping.findForward("form");
	}
	
	public ActionForward search(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		CommunityForm communityform = (CommunityForm)form;
		
		//기본설정
		setting(communityform);
		
		int cono = Integer.parseInt(communityform.getSearch_community());
		int memberCount = 0;
		
		//community 테이블 매칭 DAO
		CommunityDAO communityDao = new CommunityDAO();
		
		//해당커뮤니티 코드 설정 및 기본 정보 취득--------------
		Community comm = new Community();
		comm.setCono(cono);
		Community comm2 = communityDao.getCommunity(comm);
		//------------------------------------------------------
		
		//커뮤니티 카테고리 정보취득----------------------------
		CommunityCategoryDAO ccDAO = new CommunityCategoryDAO();
		CommunityCategory cc = ccDAO.getCommunityCategory(comm2.getCcno());
		communityform.setCommunityName(cc.getCcname());
		//------------------------------------------------------
		
		//커뮤니티 링크 정보 취득-------------------------------
		CommunityLinkDAO clDAO = new CommunityLinkDAO();
		comm2.setCommunityLinks(clDAO.getCommunityLinks(cono));
		//------------------------------------------------------
		
		//현재 가입된 총 회원수 취득----------------------------
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		memberCount = mcDAO.countCommunityMembers(cono);
		communityform.setMemberCount(memberCount);
		//------------------------------------------------------
		
		
		//커뮤니티정보 설정-------------------------------------
		communityform.setCommunity(comm2);
		communityform.setMethod(null);
		//------------------------------------------------------
		return mapping.findForward("form");
	}
	
	public ActionForward modify(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
	
		//Form취득
		CommunityForm communityform = (CommunityForm)form; 
		
		//요청된 커뮤니티를 활성화 시킨다.
		String coopen = req.getParameter("coopen");
		String cono = req.getParameter("cono");
		
		
		CommunityDAO cmDAO = new CommunityDAO();
		
		Community cm = new Community();
		
		cm.setCono(Integer.parseInt(cono)); 		//커뮤니티 번호
		
		//대기공
		if(coopen.equals("0"))
		{
			//
			cm.setCoopen(2); 	//커뮤니티 공개여부
		}
		else //대기비공
		{
			//3
			cm.setCoopen(3); 	//커뮤니티 공개여부
		}
		
		//커뮤니티 갱신
		if(cmDAO.updateCoOpen(cm)) cmDAO.setCommRegistDate(cm.getCcno());
		
		//게시판에 게시물 생성
		NoticeBoardDAO noticedao = new NoticeBoardDAO();
		NoticeBoard nb = new NoticeBoard();
		
		nb.setCono(Integer.parseInt(cono));
		nb.setMid(cmDAO.getCommunity(cono).getMasterid());
		nb.setNbtitle("커뮤니티가 개설되었습니다.");
		nb.setNbcontent("앞으로 많은 이용 부탁드립니다.");
		nb.setNbcounter(0);
		noticedao.insert(nb);
		
		//기본설정
		setting(communityform);

		return mapping.findForward("form");
	}
	
	
	public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) throws Exception {
		
		//Form취득
		CommunityForm communityform = (CommunityForm)form; 
		
		//요청된 커뮤니티를 활성화 시킨다.
		String cono = req.getParameter("cono");
		
		
		CommunityDAO cmDAO = new CommunityDAO();
		MemberCommunityDAO mcDAO = new MemberCommunityDAO();
		
		Community cm = new Community();
		
		cm.setCono(Integer.parseInt(cono)); 		//커뮤니티 번호
		
		
		//커뮤니티 삭제
		//======================================
		//해당 커뮤니티 전 회원을 삭제 한다.
		mcDAO.delete(cm.getCono());
		
		//커뮤니티 삭제
		cmDAO.delete(cm.getCcno());
		//======================================
		//기본설정
		setting(communityform);

		return mapping.findForward("form");
	}
	
	
	public void setting(ActionForm form) throws Exception 
	{
		//Form취득
		CommunityForm communityform = (CommunityForm)form; 
		
		long totalCount = 0;		//전체 커뮤니티 개수
		
		//community 테이블 매칭 DAO
		CommunityDAO communityDao = new CommunityDAO();
		
		//******************************필요테이터취득
		totalCount = communityDao.getTotalRows();
		
		System.out.println(totalCount);
		communityform.setHoldonCommunitys(communityDao.getHoldonCommunityList()); //미등록 커뮤니티 정보
		communityform.setTotalCount(String.valueOf(totalCount));		    //커뮤니티 총수
		communityform.setCommunityInfo(communityDao.getSearchCommunityList());	//커뮤니티 정보
		//******************************완료
	}
}
